#include "Definitions.h"
#include "_039944228_052326816.h"

#include "Algorithem.h"
//#include ""
#include <vector>
#include "SmartSenetBoard.h"
#include "HeuristicOne.h"
#include "HeuristicTwo.h"

_039944228_052326816::_039944228_052326816(
		Definitions::PlayerColor playerColor, const GameTimer::TimeParams &tp,
		int moves_limit) :
	SenetPlayer(playerColor, tp, moves_limit) {
	setName("Tutankhamun");
}

_039944228_052326816::~_039944228_052326816() {
}


void _039944228_052326816::initGame(int num_squares, int num_pieces, 
					   int num_sticks, const SenetBoard &initial_board)
{
	m_size_squares = num_squares;
	m_num_pieces = num_pieces;
	m_num_sticks = num_sticks;

	double timePerMove = m_gameTimerParams.timePerMove;

	if (timePerMove <= 2.0)
		Algorithem::curBestDepth = 5;
	else if (timePerMove <= 6.0)
		Algorithem::curBestDepth = 6;
	else 
		Algorithem::curBestDepth = 7;
	//else if (timePerMove < 15.0)
	//	Algorithem::curBestDepth = 7;
	//else 
	//	Algorithem::curBestDepth = 7;
}	

void _039944228_052326816::makeMove(const Board& curBoard, Move &myMove) {
	const SenetBoard &kb(dynamic_cast<const SenetBoard&> (curBoard));
	vector<SenetMove> succ(kb.getSuccessors(m_myColor));
	GameTimer my_timer(m_gameTimerParams);
	my_timer.startMoveTimer();
	//	km = succ[RandInt(succ.size()) - 1];

	//	vector<int> v;
	//	v.resize(5);
	//	v[0]= 1;


	//creating smart board
	SmartSenetBoard skb;
	skb.m_size_squares = m_size_squares;
	skb.m_num_pieces = m_num_pieces;
	skb.m_num_sticks = m_num_sticks;
	skb.m_boardState.resize(m_size_squares);
	skb.m_num_extra_safe = -3;
	skb.m_curPlayer = kb.getCurPlayerColor();
	for (int i = 0; i < m_size_squares; i++) {

		skb.m_boardState[i].player_color = kb.getBoardState(i).player_color;
		skb.m_boardState[i].rebirth_id = kb.getBoardState(i).rebirth_id;
		skb.m_boardState[i].rebirth_to = kb.getBoardState(i).rebirth_to;
		skb.m_boardState[i].square_type
				= (SmartSenetBoard::SquareType) kb.getBoardState(i).square_type;
		if (skb.m_boardState[i].square_type == SmartSenetBoard::SAFE)
			skb.m_num_extra_safe++;
	}
	SenetChanceState senetCahnceState = kb.getSticksState();
	skb.setChanceState(senetCahnceState);

	SenetMove &km(dynamic_cast<SenetMove&> (myMove));
	Heuristic* h;
	if (m_myName.compare("Tutankhamun") == 0){
		h = new HeuristicTwo(skb);
		km.m_square = Algorithem::startIteritiveStarTwo(skb, my_timer, h);
	}
	else{
		h = new HeuristicOne(skb);
		km.m_square = Algorithem::startExpectiMax(skb, my_timer, h);
	}
	

	//km.m_square = Algorithem::startExpectiMax(skb, my_timer, h);
	//km.m_square = Algorithem::startStarOne(skb, my_timer, h);
	//km.m_square = Algorithem::startIteritiveStarOne(skb, my_timer, h);
	
	delete h;
		

#ifdef __AI_GAME_RANDOM_PLAYER_DEBUG__
	vector<string> players_names;
	if (m_myColor == Definitions::WHITE) {
		players_names.push_back(getName());
		players_names.push_back("OPPONENT");
	} else {
		players_names.push_back("OPPONENT");
		players_names.push_back(getName());
	}

	kb.drawBoard(players_names);

	printf("%s ", getName().c_str());
	printf("[%s (%c)] ", Definitions::playerColorToString(m_myColor).c_str(),
			Definitions::playerColorToChar(m_myColor));
	printf("to move.\n");
	printf("Current sticks state: %d\n", kb.getSticksState());
	printf("Legal moves: ");
	for (unsigned int i(0); i < succ.size(); ++i)
		printf("%d ", succ[i].m_square);
	printf("\n");

	printf("Move taken: %d\n", km.m_square);
#endif

#ifdef __AI_GAME_SYSTEM_CONSOLE_DEBUG__
	printf("Move: %s (%d)\n", Definitions::playerColorToString(m_myColor).c_str(), km.m_square);
#endif
	//	printf("blabla\n");
}

void _039944228_052326816::setName(const string &s) {
	m_myName = s;
}

